#include <bits/stdc++.h>
using namespace std;

using pii = pair<int, int>;

int const DR[] = {-1, 1, 0, 0};
int const DC[] = {0, 0, -1, 1};

int N, M;
vector<string> Board;
vector<vector<int>> Flag;

int proc(){
    Flag.assign(N, vector<int>(M, 0));
    queue<pii> q;
    q.push({0, 0});
    Flag[0][0] = 1;

    int ans = 0;
    int sz = 0;
    while(sz = q.size()){
        ++ans;
        while(sz--){
            auto h = q.front(); q.pop();
            for(int nx,ny,i=0;i<4;++i){
                nx = h.first + DR[i];
                ny = h.second + DC[i];
                if(0 <= nx and nx < N and 0 <= ny and ny < M and Board[nx][ny] != Board[h.first][h.second] and 0 == Flag[nx][ny]){
                    Flag[nx][ny] = 1;
                    if(nx == N - 1 and ny == M - 1) return ans;
                    q.push({nx, ny});
                }      
            }
        }
    }
//     assert(0);
    return -1;
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("z.txt", "r", stdin);
#endif
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> N >> M;
    Board.assign(N, "");
    for(auto & s : Board) cin >> s;
    cout << proc() << endl;

    return 0;
}
